package com.wangcang.widgetlibrary.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.eventbus_event.ScanResultEvent;
import com.wangcang.widgetlibrary.tools.PermissionUtils;

import java.util.List;

import de.greenrobot.event.EventBus;

public class QRCodeActivity extends Activity implements PermissionUtils.PermissionCallbacks {

    Button scanBt = null;
    TextView resultTextView = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode);

        EventBus.getDefault().register(this);
        scanBt = (Button) findViewById(R.id.scan_bt);
        resultTextView = (TextView) findViewById(R.id.text);

        scanBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtils.requestPermissions(QRCodeActivity.this, new String[] {Manifest.permission.CAMERA}, 1);
            }
        });
    }

    // 二维码扫描接收
    public void onEventMainThread(ScanResultEvent event) {
        resultTextView.setText(event.getMsg());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Intent it = new Intent(QRCodeActivity.this, CaptureActivity.class);
        startActivity(it);
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }
}
